package com.zkt.recommend.domain.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjUtil;
import com.zkt.recommend.domain.convert.RecommendUserPortraitConverter;
import com.zkt.recommend.domain.entity.RecommendUserPortraitBO;
import com.zkt.recommend.domain.service.RecommendUserPortraitDomainService;
import com.zkt.recommend.infra.basic.entity.RecommendUserPortrait;
import com.zkt.recommend.infra.basic.entity.UserInfo;
import com.zkt.recommend.infra.basic.service.IRecommendUserPortraitService;
import com.zkt.recommend.infra.entity.SubjectLabelInfo;
import com.zkt.recommend.infra.rpc.SubjectRPC;
import com.zkt.recommend.infra.rpc.UserRPC;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/29
 * @description 用户画像service层实现类
 **/

@Service
public class RecommendUserPortraitDomainServiceImpl implements RecommendUserPortraitDomainService {
	
	@Resource
	private IRecommendUserPortraitService recommendUserPortraitService;
	
	@Resource
	private UserRPC userRPC;
	
	@Resource
	private SubjectRPC subjectRPC;
	
	/**
	 * 获取用户画像
	 *
	 * @param username 用户名/账号
	 * @return 是否成功
	 */
	@Override
	public List<RecommendUserPortraitBO> getUserPortrait(String username) {
		UserInfo userInfo = userRPC.getUserInfo(username);
		
		List<RecommendUserPortrait> recommendUserPortraitList = recommendUserPortraitService.lambdaQuery()
				.eq(RecommendUserPortrait::getUserId, userInfo.getId())
				.list();
		
		if (CollUtil.isEmpty(recommendUserPortraitList)) {
			return ListUtil.empty();
		}
		
		List<RecommendUserPortraitBO> recommendUserPortraitBOList =
				RecommendUserPortraitConverter.INSTANCE.convertUserPortraitToBoList(recommendUserPortraitList);
		Map<Long, RecommendUserPortraitBO> boMap =
				recommendUserPortraitBOList.stream().collect(Collectors.toMap(RecommendUserPortraitBO::getLabelId, t -> t));
		
		List<Long> labelIdList = recommendUserPortraitList.stream().map(RecommendUserPortrait::getLabelId).toList();
		
		List<SubjectLabelInfo> subjectLabelInfoList = subjectRPC.getLabelsByIds(labelIdList);
		
		subjectLabelInfoList.forEach(subjectLabelInfo -> {
			if (boMap.containsKey(subjectLabelInfo.getId())) {
				boMap.get(subjectLabelInfo.getId()).setLabelName(subjectLabelInfo.getLabelName());
			}
		});
		
		return boMap.values().stream().sorted(Comparator.comparingInt(RecommendUserPortraitBO::getMasteryScore)).toList();
	}
	
	/**
	 * 删除用户画像
	 *
	 * @param recommendUserPortraitBO 删除条件
	 * @return 是否成功
	 */
	@Override
	public Boolean remove(RecommendUserPortraitBO recommendUserPortraitBO) {
		RecommendUserPortrait recommendUserPortrait =
				RecommendUserPortraitConverter.INSTANCE.convertBoToUserPortrait(recommendUserPortraitBO);
		
		return recommendUserPortraitService.lambdaUpdate()
				.eq(ObjUtil.isNotNull(recommendUserPortrait.getId()), RecommendUserPortrait::getId, recommendUserPortrait.getId())
				.eq(ObjUtil.isNotNull(recommendUserPortrait.getUserId()), RecommendUserPortrait::getUserId,
						recommendUserPortrait.getUserId())
				.eq(ObjUtil.isNotNull(recommendUserPortrait.getLabelId()), RecommendUserPortrait::getLabelId,
						recommendUserPortrait.getLabelId())
				.remove();
	}
}